﻿using BCCommon;
using BCCommon.OAEnums.TeamWork;
using BCDto.OA.Common;
using BCDto.OA.TeamWork.TeamWork;
using BCDto.OA.TeamWork.TeamWorkParticipant;
using BCDto.OA.TeamWork.TeamWorkReply;
using BCService.OA.TeamWork.TeamWorkService;
using BlackCoffeeRESTful.Configs;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;

namespace BlackCoffeeRESTful.Controllers.Management.OA.TeamWork
{
    /// <summary>
    /// 协同办公
    /// </summary>
    [ApiExplorerSettings(GroupName = "OAAdmin")]
    [Route("api/v1/sim/[controller]")]
    public class TeamWorkController : ManagementBaseController
    {
        private readonly ITeamWorkService teamWorkService;

        public TeamWorkController(ITeamWorkService teamWorkService)
        {
            this.teamWorkService = teamWorkService;
        }

        #region 协同办公
        #region 添加协同办公
        /// <summary>
        ///  添加协同办公
        /// </summary>
        /// <param name="requestDto">协同办公信息</param>
        /// <response code="201">添加协同办公成功</response>
        /// <returns></returns>
        [HttpPost]
        [Log("添加协同办公")]
        [ProducesResponseType(typeof(TeamWorkDto), 201)]
        public IActionResult Post([FromBody] TeamWorkRequestDto requestDto)
        {
            var result = this.teamWorkService.Add(requestDto);
            return this.Created(string.Format("api/v1/sim/TeamWork/{0}", result.TeamWorkId), result);
        }
        #endregion

        #region 修改协同办公
        /// <summary>
        ///  修改协同办公
        /// </summary>
        /// <param name="teamWorkId">协同办公Id</param>
        /// <param name="requestDto">协同办公信息</param>
        /// <response code="200">修改协同办公成功</response>
        /// <returns></returns>
        [HttpPut("{team_work_id}")]
        [Log("修改协同办公")]
        [ProducesResponseType(typeof(TeamWorkDto), 200)]
        public IActionResult Put([FromRoute(Name = "team_work_id")] long teamWorkId, [FromBody] TeamWorkRequestDto requestDto)
        {
            return this.Ok(this.teamWorkService.Update(teamWorkId, requestDto));
        }
        #endregion

        #region 完成协同办公
        /// <summary>
        ///  完成协同办公
        /// </summary>
        /// <param name="teamWorkId">协同办公Id</param>
        /// <param name="requestDto">处理备注</param>
        /// <response code="200">修改协同办公成功</response>
        /// <returns></returns>
        [HttpPut("{team_work_id}/[action]")]
        [Log("完成协同办公")]
        [ProducesResponseType(typeof(TeamWorkDto), 200)]
        public IActionResult Finish([FromRoute(Name = "team_work_id")] long teamWorkId, [FromBody] RemarkRequestDto requestDto)
        {
            return this.Ok(this.teamWorkService.UpdateFinish(teamWorkId, requestDto));
        }
        #endregion

        #region 关闭协同办公
        /// <summary>
        ///  关闭协同办公
        /// </summary>
        /// <param name="teamWorkId">协同办公Id</param>
        /// <param name="requestDto">处理备注</param>
        /// <response code="200">修改协同办公成功</response>
        /// <returns></returns>
        [HttpPut("{team_work_id}/[action]")]
        [Log("关闭协同办公")]
        [ProducesResponseType(typeof(TeamWorkDto), 200)]
        public IActionResult Close([FromRoute(Name = "team_work_id")] long teamWorkId, [FromBody] RemarkRequestDto requestDto)
        {
            return this.Ok(this.teamWorkService.UpdateClose(teamWorkId, requestDto));
        }
        #endregion

        #region 删除协同办公
        /// <summary>
        ///  删除协同办公
        /// </summary>
        /// <param name="teamWorkId">协同办公Id</param>
        /// <response code="204">删除协同办公成功</response>
        /// <returns></returns>
        [HttpDelete("{team_work_id}")]
        [Log("删除协同办公")]
        public IActionResult Delete([FromRoute(Name = "team_work_id")] long teamWorkId)
        {
            if (!this.Admin.IsSystemAccount)
            {
                throw new ArgumentException("此功能仅支持总经理删除");
            }
            this.teamWorkService.Delete(teamWorkId);
            return this.NoContent();
        }
        #endregion

        #region 获取协同办公
        /// <summary>
        ///  获取协同办公
        /// </summary>
        /// <param name="teamWorkId">协同办公Id</param>
        /// <response code="200">获取协同办公</response>
        /// <returns></returns>
        [HttpGet("{team_work_id}")]
        [ProducesResponseType(typeof(TeamWorkViewDto), 200)]
        public IActionResult Get([FromRoute(Name = "team_work_id")] long teamWorkId)
        {
            return this.Ok(this.teamWorkService.GetEntity(teamWorkId));
        }

        /// <summary>
        ///  获取协同办公（分页）
        /// </summary>
        /// <param name="referenceNo">引用编号</param>
        /// <param name="workTitle">工作标题</param>
        /// <param name="teamWorkCategoryCode">协同办公分类编码</param>
        /// <param name="workStatus">工作状态</param>
        /// <param name="start">开始时间</param>
        /// <param name="end">结束时间</param>
        /// <param name="pageIndex">以 1 起始的页码 (默认 1)。</param>
        /// <param name="pageSize">每页要显示的行数 (默认 10)。</param>
        /// <response code="200">获取协同办公（分页）</response>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(PageableList<TeamWorkViewDto>), 200)]
        public IActionResult Query([FromQuery(Name = "reference_no")] string referenceNo,
            [FromQuery(Name = "work_title")] string workTitle,
            [FromQuery(Name = "team_work_category_code")] string teamWorkCategoryCode,
            [FromQuery(Name = "work_status")] OAWorkStatus? workStatus,
            [FromQuery(Name = "start")] DateTime? start,
            [FromQuery(Name = "end")] DateTime? end,
            int pageIndex = 1,
            int pageSize = 10)
        {
            return this.Ok(this.teamWorkService.Query(referenceNo, workTitle, teamWorkCategoryCode, workStatus, start, end, pageIndex, pageSize));
        }
        #endregion
        #endregion

        #region 获取协同办公参与人列表
        /// <summary>
        ///  获取协同办公参与人列表
        /// </summary>
        /// <param name="teamWorkId">协同办公Id</param>
        /// <response code="200">获取协同办公参与人列表</response>
        /// <returns></returns>
        [HttpGet("{team_work_id}/[action]")]
        [ProducesResponseType(typeof(IEnumerable<TeamWorkParticipantDto>), 200)]
        public IActionResult Participants([FromRoute(Name = "team_work_id")] long teamWorkId)
        {
            return this.Ok(this.teamWorkService.GetParticipantList(teamWorkId));
        }

        /// <summary>
        ///  获取协同办公回复记录
        /// </summary>
        /// <param name="teamWorkId">协同办公Id</param>
        /// <response code="200">获取协同办公回复记录</response>
        /// <returns></returns>
        [HttpGet("{team_work_id}/[action]")]
        [ProducesResponseType(typeof(IEnumerable<TeamWorkReplyDto>), 200)]
        public IActionResult Replies([FromRoute(Name = "team_work_id")] long teamWorkId)
        {
            return this.Ok(this.teamWorkService.GetReplyList(teamWorkId));
        }
        #endregion

        #region 获取协同办公任务数量
        /// <summary>
        ///  获取协同办公任务数量
        /// </summary>
        /// <param name="teamWorkCategoryCode">协同办公任务编码</param>
        /// <response code="200">获取协同办公任务数量</response>
        /// <returns></returns>
        [HttpGet("[action]/{team_work_category_code}")]
        [ProducesResponseType(typeof(int), 200)]
        public IActionResult ProcessingCount([FromRoute(Name = "team_work_category_code")] string teamWorkCategoryCode)
        {
            return this.Ok(this.teamWorkService.GetProcessingCount(this.Admin.AdminId, teamWorkCategoryCode));
        }
        #endregion
    }
}

